/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heraclito;

import java.io.Serializable;
import logic.Formula;
import logic.ParserException;
import logic.ScannerException;

/**
 *
 * @author Usuário
 */

public class Linha implements Serializable{

	private static final long serialVersionUID = -8773258469234439322L;
    private Formula formula;
    private String pos;
    private String regra;
    private int qtPCRAA;
    private int indice;
    private boolean usable;
    
    public boolean isUsable() {
		return usable;
	}
    
    public void setUnusable() {
		this.usable = false;
	}
    
    public Linha(){}
    
    public Linha(String formula,String pos,String regra,int qtPCRAA,int indice)
    {
        this.formula = new Formula();
        this.parse(formula);
        this.pos=pos;
        this.regra=regra;
        this.qtPCRAA=qtPCRAA;
        this.indice=indice;
        this.usable = true;
    }
    
    
    public Linha(Formula formula,String pos,String regra,int qtPCRAA,int indice)
    {
        this.formula = formula;
        this.parse(formula.display());
        this.pos=pos;
        this.regra=regra;
        this.qtPCRAA=qtPCRAA;
        this.indice=indice;
        this.usable = true;
    }
    
    private void parse(String formula)
    {
        try {
            this.formula.parse(formula);
        } catch (ParserException ex) {
            return;
        } catch (ScannerException ex) {
            return;
        }
    }
    @Override
    public String toString()
    {
        String msg=""+indice;
        int tam=4-msg.length();
        for(int x=0;x<tam;++x) msg+=" ";
        for(int x=0;x<qtPCRAA;++x) msg+="| ";
        msg+=formula.display();
        tam=90-msg.length();//39
        for(int x=0;x<tam;++x) msg+=" ";
        msg+=" "+regra;
        if(pos!=null) msg+=pos;
        return msg;
    }

    public void selected()
    {
        if(pos==null)pos="  <<<<<";
        else pos+="  <<<<<";
    }
    
    public void unselect()
    {
        if(pos!=null) if(pos.contains("  <<<<<")) pos=pos.substring(0,pos.length()-7);
    }
    
    public String salvar()
    {
        String msg=" \n"+formula.getProposition()+"\n";
        msg+=pos+"\n";
        msg+=regra+"\n";
        msg+=qtPCRAA+"\n";
        msg+=indice;
        return msg;
    }
    
    public String getProposition() {
        return formula.display();
    }

    public int getIndice() {
        return indice;
    }

    public String getPos() {
        return pos;
    }

    public int getQtPCRAA() {
        return qtPCRAA;
    }

    public String getRegra() {
        return regra;
    }

    public Formula getFormula() {
        return formula;
    }

    public void setProposition(String formula) {
        this.parse(formula);
    }

    public void setIndice(int indice) {
        this.indice = indice;
    }

    public void setFormula(Formula formula) {
        this.formula = formula;
    }

    public void setPos(String pos) {
        this.pos = pos;
    }

    public void setQtPCRAA(int qtPCRAA) {
        this.qtPCRAA = qtPCRAA;
    }

    public void setRegra(String regra) {
        this.regra = regra;
    }
    
    public String formata() {
        String linhasUsadas;
        if (pos.contains("-") && !pos.contains(">")) {
            linhasUsadas = pos.replace("-", ",");
        } else {
            linhasUsadas = pos;
        }
        String formulaFormatada = indice + "." + "\t" + qtPCRAA + ".\t" + formula.display() + ".\t'" + regra.substring(0, regra.length() - 1) + "'.\t" + "[" + linhasUsadas + "].";
        return formulaFormatada;
    }
}

